import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class OfflineDatabaseProvider{

  static const String TABLE_COMPANIES = 'companies';
  static const String TABLE_CATEGORIES = 'categories';
  static const String TABLE_PRODUCTS = 'products';
  static const String TABLE_VARIANTS = 'variants';
  static const String TABLE_PICTURES = 'pictures';
  static const String TABLE_SEARCH_TAGS = 'search_tags';
  static const String TABLE_FILTER_OPTIONS = 'filter_options';
  static const String TABLE_FILTER_OPTIONS_MAPS = 'filter_options_maps';
  static const String TABLE_PRODUCT_SEARCH_TAGS = 'product_search_tags';

  Future open(onValue) async {
    var dbPath = join(await getDatabasesPath(), "offline.db");
    await openDatabase(dbPath, version: 1,
        onCreate: (Database db, int version) async {
          print("onCreate");
          await db.execute(createTable(TABLE_COMPANIES, "rate1 INTEGER, id INTEGER PRIMARY KEY, deadline TEXT, user_id INTEGER, name TEXT"));
          await db.execute(createTable(TABLE_CATEGORIES, "retail_rate INTEGER, parent_id INTEGER, updated_at TEXT, id INTEGER PRIMARY KEY, name INTEGER, is_parent TEXT, position INTEGER, company_id INTEGER"));
          await db.execute(createTable(TABLE_PRODUCTS, "retail_rate INTEGER, visible INTEGER, id INTEGER PRIMARY KEY, name TEXT, product_category_id INTEGER, position INTEGER, thumb_picture TEXT, main_picture TEXT, company_id INTEGER"));
          await db.execute(createTable(TABLE_VARIANTS, "retail_rate INTEGER, product_id INTEGER, id INTEGER PRIMARY KEY, options_text TEXT, price INTEGER"));//小数价格？
          await db.execute(createTable(TABLE_PICTURES, "product_id INTEGER, id INTEGER PRIMARY KEY, file TEXT"));
          await db.execute(createTable(TABLE_SEARCH_TAGS, "name TEXT, id INTEGER PRIMARY KEY"));
          await db.execute(createTable(TABLE_FILTER_OPTIONS, "name TEXT PRIMARY KEY, value TEXT"));
          await db.execute(createTable(TABLE_FILTER_OPTIONS_MAPS, "name TEXT PRIMARY KEY, value TEXT"));
          await db.execute(createTable(TABLE_PRODUCT_SEARCH_TAGS,"search_tag_id INTEGER, id INTEGER PRIMARY KEY, source_id INTEGER, product_id INTEGER, position INTEGER, company_id INTEGER"));
        }).then(onValue);
  }



  String createTable(table, columns) {
    return "create table $table ( $columns )";
  }
}